package com.ruoyi.fangyuan.webmagic;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.fangyuan.domain.Housedetail;
import com.ruoyi.fangyuan.domain.Housing;
import com.ruoyi.fangyuan.service.HousedetailService;
import com.ruoyi.fangyuan.service.HousingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: TestPipeline
 * @Description:
 * @Auther: 龙天
 * @Version: 1.0
 * @create 2023/5/4 10:02
 */
@Component
public class TestPipeline implements Pipeline {
    @Autowired
     private HousingService housingService;
    @Autowired
    private HousedetailService housedetailService;


    @Override
    public void process(ResultItems resultItems, Task task) {
        List roomname = resultItems.get("roomname");
        List address = resultItems.get("address");
        List price = resultItems.get("price");
        List tags = resultItems.get("tags");
        List urls = resultItems.get("urls");
        List name = resultItems.get("name");
        List otherName = resultItems.get("otherName");
        List<Housing> list = new ArrayList<>();

        for(int i = 0;i<roomname.size();i++) {
            Housing webHouse = new Housing();
            webHouse.setRoomname(roomname.get(i)+"");
            webHouse.setAddress(address.get(i)+"");
            webHouse.setPrice(price.get(i)+"");
            webHouse.setTags(tags.get(i)+"");
            webHouse.setUrls(urls.get(i)+"");
            list.add(webHouse);



        }
        boolean b = housingService.saveall(list);
        if (b) {
            System.out.println("数据已成功批量添加进数据库");
        }else {
            System.err.println("数据已成功批量添加进数据库");
        }

        if (ObjectUtil.isAllNotEmpty(name,otherName)) {
            for (int j=0;j<name.size();j++){
                Housedetail housedetail = new Housedetail();
                housedetail.setName(name+"");
                housedetail.setName(otherName+"");
                housedetailService.save(housedetail);
            }
        }




    }
}
